#!/bin/sh
#Barry Kauler, May 2012, license GPL3 (ref: /usr/share/doc/legal)
#this code is taken out of /etc/rc.d/rc.sysinit
#called from /usr/bin/xwin, if file /tmp/simple_network_setup/network_default_reconnect_required_flag exists.
#that 'flag' file is created in /usr/sbin/hostname-set, if the hostname was changed and the network connection brought down.
#170309 rerwin: check currently running network manager, in case multiple setups tried; remove pwireless2 test; remove gkdial test.
#170515 rerwin: add checks for netwiz & sns installed.
#170717 update ethernet check per quirky's 161215 fix.
#170724 accommodate absence of a current exec, for default eth0.
#180104 clear resolv.conf at bootup, to ensure detection of initial change.
#180112 recode netchoice determination for SNS & netwiz.

#choose default network tool...
if [ -f /root/.connectwizardrc ];then #170309... #170724...
 . /root/.connectwizardrc #sets CURRENT_EXEC
else
 CURRENT_EXEC='connectwizard' #for this script only, not saved
fi #170724 end

NETCHOICE='connectwizard' #180112
case "$CURRENT_EXEC" in #170309 end
 sns) #180112...
  [ -x /usr/sbin/sns ] \
   && [ -s /etc/simple_network_setup/connections ] \
   && NETCHOICE='sns'
  ;;
 net-setup.sh)
  [ -x /usr/sbin/net-setup.sh ] \
   && [ "`ls -1 /etc/network-wizard/network/interfaces 2>/dev/null`" ] \
   && NETCHOICE='net-setup.sh'
  ;; #180112 end
 connectwizard|frisbee) #try determine which tool was used to setup networking... 101007 160609...
  if [ -x /usr/local/bin/frisbee ] \
    && frisbee --test_active; then #130104
   NETCHOICE='frisbee' #130104
  elif [ -x /usr/sbin/sns ] \
   && [ -s /etc/simple_network_setup/connections ];then #100306 ...160609 170515
   NETCHOICE='sns'
  elif [ -x /usr/sbin/net-setup.sh ] \
   && [ "`ls -1 /etc/network-wizard/network/interfaces 2>/dev/null`" ];then #170515
   NETCHOICE='net-setup.sh'
  fi
  ;;
esac

if [ ! -f /tmp/.initial_resolv.conf_cleared ];then #180104...
 echo -n '' > /etc/resolv.conf
 touch /tmp/.initial_resolv.conf_cleared
fi

case $NETCHOICE in
 net-setup.sh)
  /etc/rc.d/rc.network &
 ;;
 sns) #100306
  /etc/rc.d/rc.network_basic #this only sets up interface 'lo'.
  /usr/local/simple_network_setup/rc.network &
 ;;
 connectwizard) #101007 shinobar
  #170717 (161215) rewritten...
  /etc/rc.d/rc.network_basic #this only sets up interface 'lo'.
  /etc/rc.d/rc.network_eth &   #test for wired network.
 ;;
 *) #101007 shinobar
  /etc/rc.d/rc.network_basic &
 ;;
esac
